import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useLoadingStore = defineStore('loading', () => {
  const loading = ref(false)
  let loadingTimer = null

  const showLoading = () => {
    // 清除之前的定时器
    if (loadingTimer) {
      clearTimeout(loadingTimer)
    }

    // 延迟2秒显示loading
    loadingTimer = setTimeout(() => {
      loading.value = true
    }, 500)
  }

  const hideLoading = () => {
    // 清除定时器
    if (loadingTimer) {
      clearTimeout(loadingTimer)
      loadingTimer = null
    }
    loading.value = false
  }

  return {
    loading,
    showLoading,
    hideLoading
  }
})
